﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Advisor_ManageAdmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["isAdmin"] == null)
            {
                Page.Response.Redirect("Default.aspx");
            }
            else if (!(Boolean)Session["isAdmin"])
            {
                Response.Redirect("~/Advisor/Default.aspx");
            }
            List<Dictionary<String, Object>> allAdvisors = StoredProcedures.getAllAdvisors();
            //if advisor is admin, add them to list to remove, if not admin, add them to list to add
            foreach (Dictionary<String, Object> a in allAdvisors)
            {
                if (Convert.ToInt32(a["isAdministrator"]) == 1)
                {
                    ListItem admin = new ListItem(a["lastName"] + ", " + a["firstName"], (String)a["employeeID"]);
                    AdminList.Items.Add(admin);
                }
                else
                {
                    ListItem advisor = new ListItem(a["lastName"] + ", " + a["firstName"], (String)a["employeeID"]);
                    AdvisorList.Items.Add(advisor);
                }
            }
            List<Dictionary<String, Object>> formerAdvisorsResult = StoredProcedures.getFormerAdvisors();
            foreach (Dictionary<String, Object> b in formerAdvisorsResult)
            {
                ListItem formerA = new ListItem(b["lastName"] + ", " + b["firstName"], (String)b["employeeID"]);
                FormerAdvisors.Items.Add(formerA);
            }
            List<Dictionary<String, Object>> formerElectivesResult = StoredProcedures.getFormerElectives();
            foreach( Dictionary<String, Object> el in formerElectivesResult){
                ListItem formerE = new ListItem("" + el["name"], el["electiveID"].ToString());
                FormerElectives.Items.Add(formerE);
            }

        }
 
    }//end Page_Load()


    protected void HomeButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Default.aspx");
    }

    protected void HelpButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Help.aspx");
    }


    protected void RemoveButton_Click(object sender, EventArgs e)
    {
        if (AdminList.SelectedItem.Value == Session["Advisor_advisorID"].ToString())
        {
            MessageBox.Show("You are not allowed to remove yourself as an Admin");
            return;
        }
        if (AdminList.SelectedItem == null)
        {
            MessageBox.Show("Please select a current Admin to remove");
            return;
        }

        if (StoredProcedures.demoteFromAdmin(AdminList.SelectedItem.Value) == StoredProcedures.FAILURE)
        {
            MessageBox.Show("There was an error removing the administrator");
            return;
        }
        else
        {
            AdvisorList.Items.Add(AdminList.SelectedItem);
            AdminList.Items.Remove(AdminList.SelectedItem);
            AdminList.ClearSelection();
        }
    }//end RemoveButton_Click

    protected void AddButton_Click(object sender, EventArgs e)
    {
        if (AdvisorList.SelectedItem == null)
        {
            MessageBox.Show("Please select a current Advisor to promote");
            return;
        }

        if (StoredProcedures.promoteToAdmin(AdvisorList.SelectedItem.Value) == StoredProcedures.FAILURE)
        {
            MessageBox.Show("There was an error promoting the advisor");
            return;
        }
        else
        {
            AdminList.Items.Add(AdvisorList.SelectedItem);
            AdvisorList.Items.Remove(AdvisorList.SelectedItem);
            AdvisorList.ClearSelection();
        }
    }//end AddButton_Click()

    protected void PurgeAdvisorButton_Click(object sender, EventArgs e)
    {
        if (FormerAdvisors.SelectedItem == null)
        {
            MessageBox.Show("You must select a former Advisor before removing them");
            return;
        }

        if (StoredProcedures.removeFormerAdvisor(FormerAdvisors.SelectedItem.Value) == StoredProcedures.FAILURE)
        {
            MessageBox.Show("There was an error removing the advisor");
            return;
        }
        else
        {
            FormerAdvisors.Items.Remove(AdvisorList.SelectedItem);
            FormerAdvisors.ClearSelection();
        }
    }//end PurgeButton_Click()

    protected void MergeAdviseesButton_Click(object sender, EventArgs e)
    {
        int numModified = StoredProcedures.mergeAdvisees(); 
        if (numModified != StoredProcedures.FAILURE)
        {
            MessageBox.Show(numModified + " Advisees successfull updated");
        }
    }//end MergeAdviseesButton_Click()

    protected void MergeEmployeesButton_Click(object sender, EventArgs e)
    {
        int numModified = StoredProcedures.mergeEmployees();
        if (numModified != StoredProcedures.FAILURE)
        {
            MessageBox.Show(numModified + " Advisors successfully updated");
        }
    }//end MergeAdviseesButton_Click()

    protected void MergeCoursesButton_Click(object sender, EventArgs e)
    {
        int numModified = StoredProcedures.mergeCourses();
        if (numModified != StoredProcedures.FAILURE)
        {
            MessageBox.Show(numModified + " Courses successfully updated");
        }
    }//end MergeCoursesButton_Click()

    protected void MergePrereqsButton_Click(object sender, EventArgs e)
    {
        int numModified = StoredProcedures.mergePrereqs();
        if (numModified != StoredProcedures.FAILURE)
        {
            MessageBox.Show(numModified + " Prerequisites successfully updated");
        }
    }//end MergeCoursesButton_Click()

    protected void MergeCoreqsButton_Click(object sender, EventArgs e)
    {
        int numModified = StoredProcedures.mergeCoreqs();
        if (numModified != StoredProcedures.FAILURE)
        {
            MessageBox.Show(numModified + " Coreqsuisites successfully updated");
        }
    }//end MergeCoursesButton_Click()

    protected void PurgeElectiveButton_Click(object sender, EventArgs e)
    {
        if (FormerElectives.SelectedItem == null)
        {
            MessageBox.Show("Please select an Elective to permanently delete");
            return;
        }

        if (StoredProcedures.removeElective(FormerElectives.SelectedItem.Value) == StoredProcedures.FAILURE)
        {
            MessageBox.Show("There was an error deleting the elective");
            return;
        }
        else
        {
            FormerElectives.Items.Remove(FormerElectives.SelectedItem);
            FormerElectives.ClearSelection();
        }

    }//end PurgeElectiveButton_Click()

    private void SetInitialView()
    {
        //HeaderText.Text = "Manage Admins";
        ManageTable0.Visible = true;
        ManageTable0.Enabled = true;
        ManageTable1.Visible = true;
        ManageTable1.Enabled = true;
        ManageTable2.Visible = true;
        ManageTable2.Enabled = true;
        ManageTable3.Visible = true;
        ManageTable3.Enabled = true;
        ManageTable4.Visible = true;
        ManageTable4.Enabled = true;
        ManageTable5.Visible = true;
        ManageTable5.Enabled = true;
        ManageTable6.Visible = true;
        ManageTable6.Enabled = true;
        ManageTable7.Visible = true;
        ManageTable7.Enabled = true;
    }
}